package com.sky.mq;


import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Component
@RocketMQMessageListener(topic = "delay_order",consumerGroup = "order-consumer")
public class OrderCancleConsumer implements RocketMQListener<Long> {
    @Resource
    OrderMapper orderMapper;

    @Override
    public void onMessage(Long orderId) {
        // 延迟订单时间到，查询订单状态，如果没有支付，那就取消订单
        // 根据id查询订单
        Orders ordersDB = orderMapper.getById(orderId);

        // 超时未支付，取消订单
        if(ordersDB.getPayStatus() == 0){
            // 根据订单id更新订单状态、取消原因、取消时间
            Orders orders = new Orders();
            orders.setId(orderId);
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelReason("订单超时，自动取消");
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.update(orders);
        }

    }
}
